package com.haiyou.common.http.request;

import java.io.Serializable;

import com.alibaba.fastjson2.JSONObject;
import com.haiyou.common.constants.ErrCode;
import com.haiyou.common.http.annotation.StringValid;
import com.haiyou.common.util.check.Assert;
import com.haiyou.common.util.encryption.RSADecrypt;
import com.haiyou.common.util.encryption.RSAEncrypt;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 
 * 
 * @Description: 加密请求
 * @author xingyuan
 * @date 2022年11月1日 下午4:01:21
 */
@Getter
@Setter
@ToString
public abstract class RSAEncryptRequest<T extends RSAEncryptRequest<?>> implements Serializable{

	/**
	 * 加密后数据
	 */
	@StringValid(notNull = true)
	private String data;
	
	/**
	 * 解密
	 * @param decrypt
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T decrypt(RSADecrypt decrypt) {
		Assert.isTrue(decrypt != null, ErrCode.ERROR_PARAMETER,"decrypt not null");
		String jsonStr = decrypt.decrypt(this.getData());
		return (T)JSONObject.parseObject(jsonStr, this.getClass());
	}
	
	/**
	 * 加密
	 * @param t
	 * @return
	 */
	public void encrypt(RSAEncrypt encrypt) {
		Assert.isTrue(encrypt != null, ErrCode.ERROR_PARAMETER,"encrypt not null");
		this.setData(encrypt.encrypt(JSONObject.toJSONString(this)));
	}
	
}
